package giro.general.stage;

import giro.general.BaseActivity;
import giro.general.R;
import giro.general.adapter.StageDetailGalleryAdapter;
import giro.general.util.Commons;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.Gallery;
import android.widget.TextView;

public class StageDetailActivity extends BaseActivity {

	private StageDetailGalleryAdapter mStageDetailGalleryAdapter;
	private Gallery mStageDetailGallery;
	private JSONArray mTappeArray;
	private TextView mIndexFormattedText;
	private StageDetailActivity mStageDetailActivity;
	private Button mDetailButton;
	private Button mSaliteButton;
	private Button mAltimetriaButton;
	private Boolean mFromFinder;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.stage_detail);
		initToolbar();
		init();

		mStageDetailActivity = this;

		int tappaPosition = 0;
		if (!mFromFinder) {
			tappaPosition = Commons.getInstance().getSelectTappaPosition();
		}
		mStageDetailGallery.setAdapter(mStageDetailGalleryAdapter);
		mStageDetailGallery.setSelection(tappaPosition);
		mHandler.post(new Runnable() {

			public void run() {
				mStageDetailGalleryAdapter.notifyDataSetChanged();
				mIndexFormattedText.setText(Integer.toString((1 + mStageDetailGallery.getSelectedItemPosition())));
			}
		});

		try {
			JSONObject object = mTappeArray.getJSONObject(mStageDetailGallery.getSelectedItemPosition());
			if (object.getJSONArray("salite").length() == 0) {
				mSaliteButton.setVisibility(View.GONE);
			} else {
				mSaliteButton.setVisibility(View.VISIBLE);
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}

		mStageDetailGallery.setOnItemSelectedListener(new Gallery.OnItemSelectedListener() {

			public void onItemSelected(AdapterView<?> arg0, View arg1, final int position, long arg3) {
			}

			public void onNothingSelected(AdapterView<?> arg0) {
			}
		});

		mStageDetailGallery.setOnTouchListener(new View.OnTouchListener() {

			public boolean onTouch(View v, MotionEvent event) {
				if (event.getAction() == MotionEvent.ACTION_UP) {
					mIndexFormattedText.postDelayed(new Runnable() {

						public void run() {
							mIndexFormattedText.setText(Integer.toString((1 + mStageDetailGallery
									.getSelectedItemPosition())));
							try {
								JSONObject object = mTappeArray.getJSONObject(mStageDetailGallery
										.getSelectedItemPosition());
								if (object.getJSONArray("salite").length() == 0) {
									mSaliteButton.setVisibility(View.GONE);
								} else {
									mSaliteButton.setVisibility(View.VISIBLE);
								}
							} catch (JSONException e) {
								e.printStackTrace();
							}
						}
					}, 200);
				}
				return false;
			}
		});

		OnClickListener onClickListener = new OnClickListener() {

			public void onClick(View v) {

				Commons.getInstance().setSelectItemPosition(mStageDetailGallery.getSelectedItemPosition() + 1);
				Intent intent = new Intent(mStageDetailActivity, TabView.class);
				switch (v.getId()) {
				case R.id.planimetria_button:
					intent.putExtra("fromDetail", "Planimetria");
					break;
				case R.id.salite_button:
					intent.putExtra("fromDetail", "Salite");
					break;
				case R.id.altimetria_button:
					intent.putExtra("fromDetail", "Altimetria");
					break;

				}
				startActivity(intent);
			}
		};

		mDetailButton.setOnClickListener(onClickListener);
		mAltimetriaButton.setOnClickListener(onClickListener);
		mSaliteButton.setOnClickListener(onClickListener);

	}

	private void init() {
		mFromFinder = (Boolean) getIntent().getBooleanExtra("fromFinder", false);
		mDetailButton = (Button) findViewById(R.id.planimetria_button);
		mSaliteButton = (Button) findViewById(R.id.salite_button);
		mAltimetriaButton = (Button) findViewById(R.id.altimetria_button);
		if (!mFromFinder) {
			mTappeArray = Commons.getInstance().getTappeArrayJsonBackup();
		} else {
			mTappeArray = Commons.getInstance().getTappeArrayJson();
		}
		mStageDetailGalleryAdapter = new StageDetailGalleryAdapter(this, mTappeArray);
		mStageDetailGallery = (Gallery) findViewById(R.id.stages_gallery);
		TextView index_total_text = (TextView) findViewById(R.id.stage_detail_total_text);
		index_total_text.setText(Integer.toString(mStageDetailGalleryAdapter.getCount()));
		mIndexFormattedText = (TextView) findViewById(R.id.stage_detail_index_text);

	}
}
